package com.code.leetcode._202504;

import com.code.leetcode._202503.ListNode;

/**删除链表的倒数第N个结点**/
public class DeleteNodeLastN {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if (head.next == null) return null;
        ListNode slow = head, fast = head;
        while (n-- > 0) fast = fast.next;//讲n个位置的结点取出来给fast
        if (fast == null) {
            head = slow.next;
        } else {
            while (fast.next != null) {
                slow = slow.next; fast = fast.next;
            }
            slow.next = slow.next.next;
        }
        return head;
    }


}
